package com.prs.crm.action.ca.work;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.apache.poi.hssf.record.formula.functions.Tdist;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.warning.AutoCodeAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.ca.work.ArticleTook;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.domain.ca.work.PurchaseApply;
import com.prs.crm.domain.ca.work.TookDepartment;
import com.prs.crm.domain.flow.FlowNodePerson;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.flow.WorkFlow;
import com.prs.crm.domain.flow.WorkFlowAndPerson;
import com.prs.crm.domain.flow.WorkFlowInstance;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.ShareEim;
import com.prs.crm.domain.sys.Form;
import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.ca.work.ArticleTookService;
import com.prs.crm.service.ca.work.ArticleTypesService;
import com.prs.crm.service.ca.work.DemandInventoryService;
import com.prs.crm.service.ca.work.TookDepartmentService;
import com.prs.crm.service.flow.FlowNodePersonService;
import com.prs.crm.service.flow.FlowVerifyDetailService;
import com.prs.crm.service.flow.WorkFlowAndPersonService;
import com.prs.crm.service.flow.WorkFlowInstanceService;
import com.prs.crm.service.flow.WorkFlowNodeService;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.flow.WorkFlowService;
import com.prs.crm.service.flow.impl.FlowVerifyDetailServiceImpl;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.person.ShareEimService;
import com.prs.crm.service.sys.FormService;
import com.prs.crm.service.warning.AutoCodeService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:33:12 物品领用
 */
public class ArticleTookAction extends BaseAction{

	private static final long serialVersionUID = -5000075852640278686L;
	private ArticleTookService articleTookService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private ArticleTook articleTook;
	private Application application;
	private ApplicationService ApplicationService;
	private ShenQingTypeService shenQingTypeService = new ShenQingTypeServiceImpl();

	private List<DemandInventory> demands;
	private DemandInventoryService demandInventoryService;
	
	private List<ArticleTypes> articleTypes;
	private ArticleTypesService articleTypesService;
	
	private List<TookDepartment> tookDepartment;
	private TookDepartmentService tookDepartmentService;

	private File upload;
	private String uploadFileName;
	
	private WorkFlowInstanceService workFlowInstanceService;
	
	
	private Form form;
	private FormService formService;
	private WorkFlowProcess workFlowProcess;
	private WorkFlowInstance workFlowInstance;
	private WorkFlowProcessService workflowProcessService;
	private WorkFlowProcessNode node;
	private Collection<WorkFlowProcessNode> endNodes;
	private File attach;
	private String attachFileName;
	private FlowVerifyDetail detail;
	private List<WorkFlow> workFlow;
	private WorkFlow workFlows;
	private WorkFlowService workflowService;
	private List<FlowNodePerson> nodePerson;
	private FlowNodePersonService flowNodePersonService;
	private WorkFlowNodeService workFlowNodeService;
	private FlowVerifyDetailService flowVerifyDetailService;
	private FlowVerifyDetail flowVerifyDetail;
	private WorkFlowAndPerson workFlowAndPerson;
	private WorkFlowAndPersonService workFlowAndPersonService;
	private AutoCodeService autoCodeService;
	private String codeNum;
	private List<WorkFlowAndPerson> workFlowAndPersons;
	private List<FlowVerifyDetail> flowVerifyDetails;
	
	private PersonService personService;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private Statement stmt1;
	private ResultSet rs1;
	private Statement stmt2;
	private ResultSet rs2;
	private Statement stmt3;
	private ResultSet rs3;
	private Statement stmt4;
	private ResultSet rs4;
	private Statement stmt5;
	private ResultSet rs5;
	private Statement stmt6;
	private ResultSet rs7;
	private Statement stmt7;
	private ResultSet rs8;
	private Statement stmt8;
	private ResultSet rs9;
	private Statement stmt9;
	private ResultSet rs10;
	private Statement stmt10;
	private ResultSet rs11;
	private Statement stmt11;
	private ResultSet rs12;
	private Statement stmt12;
	private ResultSet rs13;
	private Statement stmt13;
	private ResultSet rs14;
	private Statement stmt14;
	private ResultSet rs15;
	private Statement stmt15;
	private ResultSet rs16;
	private Statement stmt16;
	private ResultSet rs17;
	private Statement stmt17;
	private ResultSet rs18;
	private Statement stmt18;
	private ShareEimService shareEimService;
	private String ClassName;
	private String userName;
	private String userPass;
	private String url1;
	private String address;
	private String baseName;
	public ShareEimService getShareEimService() {
		return shareEimService;
	}
	public void setShareEimService(ShareEimService shareEimService) {
		this.shareEimService = shareEimService;
	}
	public List<WorkFlowAndPerson> getWorkFlowAndPersons() {
		return workFlowAndPersons;
	}
	public void setWorkFlowAndPersons(List<WorkFlowAndPerson> workFlowAndPersons) {
		this.workFlowAndPersons = workFlowAndPersons;
	}
	public List<FlowVerifyDetail> getFlowVerifyDetails() {
		return flowVerifyDetails;
	}
	public void setFlowVerifyDetails(List<FlowVerifyDetail> flowVerifyDetails) {
		this.flowVerifyDetails = flowVerifyDetails;
	}
	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}
	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}
	public String getCodeNum() {
		return codeNum;
	}
	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}
	public WorkFlowAndPerson getWorkFlowAndPerson() {
		return workFlowAndPerson;
	}
	public void setWorkFlowAndPerson(WorkFlowAndPerson workFlowAndPerson) {
		this.workFlowAndPerson = workFlowAndPerson;
	}
	public WorkFlowAndPersonService getWorkFlowAndPersonService() {
		return workFlowAndPersonService;
	}
	public void setWorkFlowAndPersonService(
			WorkFlowAndPersonService workFlowAndPersonService) {
		this.workFlowAndPersonService = workFlowAndPersonService;
	}
	public Form getForm() {
		return form;
	}
	public void setForm(Form form) {
		this.form = form;
	}
	public FormService getFormService() {
		return formService;
	}
	public void setFormService(FormService formService) {
		this.formService = formService;
	}
	public WorkFlowProcess getWorkFlowProcess() {
		return workFlowProcess;
	}
	public void setWorkFlowProcess(WorkFlowProcess workFlowProcess) {
		this.workFlowProcess = workFlowProcess;
	}
	public WorkFlowInstance getWorkFlowInstance() {
		return workFlowInstance;
	}
	public void setWorkFlowInstance(WorkFlowInstance workFlowInstance) {
		this.workFlowInstance = workFlowInstance;
	}
	public WorkFlowProcessService getWorkflowProcessService() {
		return workflowProcessService;
	}
	public void setWorkflowProcessService(
			WorkFlowProcessService workflowProcessService) {
		this.workflowProcessService = workflowProcessService;
	}
	public WorkFlowProcessNode getNode() {
		return node;
	}
	public void setNode(WorkFlowProcessNode node) {
		this.node = node;
	}
	public Collection<WorkFlowProcessNode> getEndNodes() {
		return endNodes;
	}
	public void setEndNodes(Collection<WorkFlowProcessNode> endNodes) {
		this.endNodes = endNodes;
	}
	public File getAttach() {
		return attach;
	}
	public void setAttach(File attach) {
		this.attach = attach;
	}
	public String getAttachFileName() {
		return attachFileName;
	}
	public void setAttachFileName(String attachFileName) {
		this.attachFileName = attachFileName;
	}
	public FlowVerifyDetail getDetail() {
		return detail;
	}
	public void setDetail(FlowVerifyDetail detail) {
		this.detail = detail;
	}
	public List<WorkFlow> getWorkFlow() {
		return workFlow;
	}
	public void setWorkFlow(List<WorkFlow> workFlow) {
		this.workFlow = workFlow;
	}
	public WorkFlow getWorkFlows() {
		return workFlows;
	}
	public void setWorkFlows(WorkFlow workFlows) {
		this.workFlows = workFlows;
	}
	public WorkFlowService getWorkflowService() {
		return workflowService;
	}
	public void setWorkflowService(WorkFlowService workflowService) {
		this.workflowService = workflowService;
	}
	public List<FlowNodePerson> getNodePerson() {
		return nodePerson;
	}
	public void setNodePerson(List<FlowNodePerson> nodePerson) {
		this.nodePerson = nodePerson;
	}
	public FlowNodePersonService getFlowNodePersonService() {
		return flowNodePersonService;
	}
	public void setFlowNodePersonService(FlowNodePersonService flowNodePersonService) {
		this.flowNodePersonService = flowNodePersonService;
	}
	public WorkFlowNodeService getWorkFlowNodeService() {
		return workFlowNodeService;
	}
	public void setWorkFlowNodeService(WorkFlowNodeService workFlowNodeService) {
		this.workFlowNodeService = workFlowNodeService;
	}
	public FlowVerifyDetailService getFlowVerifyDetailService() {
		return flowVerifyDetailService;
	}
	public void setFlowVerifyDetailService(
			FlowVerifyDetailService flowVerifyDetailService) {
		this.flowVerifyDetailService = flowVerifyDetailService;
	}
	public FlowVerifyDetail getFlowVerifyDetail() {
		return flowVerifyDetail;
	}
	public void setFlowVerifyDetail(FlowVerifyDetail flowVerifyDetail) {
		this.flowVerifyDetail = flowVerifyDetail;
	}
	public WorkFlowInstanceService getWorkFlowInstanceService() {
		return workFlowInstanceService;
	}
	public void setWorkFlowInstanceService(
			WorkFlowInstanceService workFlowInstanceService) {
		this.workFlowInstanceService = workFlowInstanceService;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}
	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}
	public List<TookDepartment> getTookDepartment() {
		return tookDepartment;
	}
	public void setTookDepartment(List<TookDepartment> tookDepartment) {
		this.tookDepartment = tookDepartment;
	}
	public TookDepartmentService getTookDepartmentService() {
		return tookDepartmentService;
	}
	public void setTookDepartmentService(TookDepartmentService tookDepartmentService) {
		this.tookDepartmentService = tookDepartmentService;
	}
	public List<DemandInventory> getDemands() {
		return demands;
	}
	public void setDemands(List<DemandInventory> demands) {
		this.demands = demands;
	}
	public DemandInventoryService getDemandInventoryService() {
		return demandInventoryService;
	}
	public void setDemandInventoryService(
			DemandInventoryService demandInventoryService) {
		this.demandInventoryService = demandInventoryService;
	}
	public List<ArticleTypes> getArticleTypes() {
		return articleTypes;
	}
	public void setArticleTypes(List<ArticleTypes> articleTypes) {
		this.articleTypes = articleTypes;
	}
	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}
	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public ArticleTook getArticleTook() {
		return articleTook;
	}
	public void setArticleTook(ArticleTook articleTook) {
		this.articleTook = articleTook;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public ArticleTookService getArticleTookService() {
		return articleTookService;
	}
	public void setArticleTookService(ArticleTookService articleTookService) {
		this.articleTookService = articleTookService;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	@Action("listArticleTook")
	public String listArticleTook(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ArticleTook.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getArticleTookService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listTookParticular")
	public String listTookParticular(){
		this.listArticleTook();
		return SUCCESS;
	}
	

	@Action(value = "saveArticleTook", results = {
			@Result(name = "success", type = "redirect", location = "listArticleTook"),
			@Result(name = "error", type = "chain", location = "editArticleTook") })
	public String saveArticleTook() throws SQLException{
		if (upload!=null) {
			this.articleTook.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		if(articleTook.getAt_applyDate()==null){
			articleTook.setAt_applyDate(new Date());
		}
		if("".equals(articleTook.getAt_billNumber())){
			this.addActionError("单号不能为空！");
			return ERROR;
		}
		articleTook.setAt_examaineState(1);
		articleTook.setAt_examinProcedure(1);
		this.getArticleTookService().save(articleTook);
		
		DemandInventory di=this.getDemandInventoryService().getDemandInventoryByNames(articleTook.getDemands().getDi_articleName());
		if (di!=null) {
			Integer num=Integer.parseInt(di.getDi_articleNumber())-articleTook.getTookNum();
			if (num<0) {
				this.addActionError("库存不足，无法操作");
				this.getArticleTookService().remove(articleTook);
				return ERROR;
			}
//			di.setDi_articleNumber(String.valueOf(num));
//			double allMoney=Double.parseDouble(di.getDi_money())-(double)articleTook.getTookNum()*Double.parseDouble(articleTook.getDemands().getDi_articleUnivalence());
//			System.out.println(allMoney);
//			di.setDi_money(String.valueOf(allMoney));
//			this.getDemandInventoryService().submit(di);
		}
		
//		application = new Application();
//		application.setAppState(1);
//		application.setType(articleTook.getType());
//		application.setAppName("["+articleTook.getType()+"]"+articleTook.getAppName());
//		application.setSqType(articleTook.getAt_examaineState());
//		application.setSpr(articleTook.getAt_examinProcedure());
//		application.setPerson(articleTook.getRecorder());
//		application.setResign("物品领用");
//		application.setShenqingTypeName("物品领用");
//		application.setArticleTook(articleTook);
//		//application.setShenQingType(this.getShenQingTypeService().getShenQingType("物品领用"));
//		this.getApplicationService().save(application);
		
		List<FlowNodePerson> fp = this.getFlowNodePersonService()
		.getFlowNodePerson(articleTook.getWorkFlow());
		for (FlowNodePerson ob : fp) {
			WorkFlowAndPerson workFlowAndPerson = new WorkFlowAndPerson();
			workFlowAndPerson.setWorkFlow(ob.getWorkFlow());
		//	workFlowAndPerson.setNodePersonId(String.valueOf(ob
		//			.getNodePersonName().getId()));
			workFlowAndPerson.setNodePersonId(ob.getNodeperson_id());
			workFlowAndPerson.setFlowstate("1");// 审签状态
			workFlowAndPerson.setFlowsequen(ob.getWorkFlowNode().getSequence()
					+ "");
			workFlowAndPerson.setType(ob.getWorkFlowNode().getType()+"");
			String[] persons=ob.getNodeperson_id().split(",");
		
			workFlowAndPerson.setPersonNum(persons.length);
			workFlowAndPerson.setArticleTook(articleTook);
			this.getWorkFlowAndPersonService().save(workFlowAndPerson);
		}
		
		return SUCCESS;
	}
	@Action(value = "saveStorages", results = {
			@Result(name = "success", type = "redirect", location = "listArticleTook"),
			@Result(name = "error", type = "chain", location = "editArticleTook") })
	public String saveStorages(){
		if (upload!=null) {
			this.articleTook.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
		}
		articleTook.setAt_examaineState(0);
		this.getArticleTookService().save(articleTook);
		application = new Application();
		application.setAppState(0);
		application.setType(articleTook.getType());
		application.setAppName("["+articleTook.getType()+"]"+articleTook.getAppName());
		application.setSqType(articleTook.getAt_examaineState());
		application.setSpr(0);
		application.setPerson(articleTook.getRecorder());
		application.setResign("物品领用");
		application.setArticleTook(articleTook);
		this.getApplicationService().save(application);
		return SUCCESS;
	}
	
	@Action(value = "removeArticleTook", results = {
			@Result(name = "success", type = "redirect", location = "listArticleTook"),
			@Result(name = "error", type = "chain", location = "listArticleTook") })
	public String removeArticleTook(){
		if (ids==null) {
			this.addActionError("请选择要删除的物品");
			return ERROR;
		}
		for (Integer id : ids) {
			ArticleTook articletook=this.getArticleTookService().get(id);
			if (articletook.getTookPar().size()!=0) {
				this.addActionError("该数据有联系不能删除！");
				return ERROR;
			}
		}
		this.getArticleTookService().batchRemove(ids);
		this.setPrompt("删除成功！");
		return SUCCESS;
	}
	
	@Action("editArticleTook")
	public String editArticleTook(){
		this.demands=this.getDemandInventoryService().getAllDemandInventory();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		this.tookDepartment=this.getTookDepartmentService().getAllDepartment();
		this.workFlow=this.getWorkflowService().getWorkFlow("办公用品流程");
		codeNum=getCode("LY");
		return SUCCESS;
	}
	
	@Action("viewTookParticular")
	public String viewTookParticular(){
		this.demands=this.getDemandInventoryService().getAllDemandInventory();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		return SUCCESS;
	}
	
	@Action("listAppArticleTook")
	public String listAppArticleTook() {
		 Person currentperson = AppContext.getAccountContext().currentUser();
		 List<ArticleTook> at=this.getArticleTookService().getAllArticleTook();
		 for (ArticleTook ob : at) {
//			 System.out.println(ob.getAt_billNumber());
			 List<WorkFlowAndPerson> wf = this.getWorkFlowAndPersonService().getwWorkFlowAndPersonByArticleTook(ob.getWorkFlow(),ob);
				//顺序调整
			 List<WorkFlowAndPerson> wff=  sequens(wf);
			 for(WorkFlowAndPerson obb:wff){
				 if("1".equals(obb.getFlowstate())) {
					 if ("1".equals(obb.getType())) {
						String[] personss=obb.getNodePersonId().split(",");
						for(String persons:personss){
//							System.out.println(persons);
							if((currentperson.getId()+"").equals(persons)){
								 if (ob.getPersonAppProcess()!=null) {
										String[] personapp=ob.getPersonAppProcess().split(",");
										 for (String pers : personapp) {
											if (currentperson.getResume().getName().equals(pers)) {
												ob.setPersonAppState(2);
												 this.getArticleTookService().submit(ob);
												break;
											}else{
												 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
												    ob.setAt_examaineState(1);
												    ob.setPersonAppState(1);
												    this.getArticleTookService().submit(ob);  
											}
										}
									}else{
										 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
									     ob.setAt_examaineState(1);
									     ob.setPersonAppState(1);
									     this.getArticleTookService().submit(ob);
									}
							}else{
								
							}
						}
						break;
					 }
				 
				 if ("2".equals(obb.getType())) {
						String[] personss=obb.getNodePersonId().split(",");
						for(String persons:personss){
//						System.out.println(persons);
							if((currentperson.getId()+"").equals(persons)){
								 if (ob.getPersonAppProcess()!=null) {
										String[] personapp=ob.getPersonAppProcess().split(",");
										 for (String pers : personapp) {
											if (currentperson.getResume().getName().equals(pers)) {
												ob.setPersonAppState(2);
												 this.getArticleTookService().submit(ob);
												break;
											}else{
												 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
												    ob.setAt_examaineState(1);
												    ob.setPersonAppState(1);
												    this.getArticleTookService().submit(ob);  
											}
										}
									}else{
										 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
										    ob.setAt_examaineState(1);
										    ob.setPersonAppState(1);
										    this.getArticleTookService().submit(ob);
									}
							}else{
								
							}
						}
						break;
				}
				 if ("3".equals(obb.getType())) {
						String[] personss=obb.getNodePersonId().split(",");
						for(String persons:personss){
//						System.out.println(persons);
							if((currentperson.getId()+"").equals(persons)){
								 if (ob.getPersonAppProcess()!=null) {
										String[] personapp=ob.getPersonAppProcess().split(",");
										 for (String pers : personapp) {
											if (currentperson.getResume().getName().equals(pers)) {
												ob.setPersonAppState(2);
												 this.getArticleTookService().submit(ob);
												break;
											}else{
												 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
												    ob.setAt_examaineState(1);
												    ob.setPersonAppState(1);
												    this.getArticleTookService().submit(ob);  
											}
										}
									}else{
										 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
										    ob.setAt_examaineState(1);
										    ob.setPersonAppState(1);
										    this.getArticleTookService().submit(ob);
									}
							}else{
								
							}
						}
						break;
					 }
				 if ("5".equals(obb.getType())) {
						String[] personss=obb.getNodePersonId().split(",");
						for(String persons:personss){
//						System.out.println(persons);
							if((currentperson.getId()+"").equals(persons)){
								 if (ob.getPersonAppProcess()!=null) {
										String[] personapp=ob.getPersonAppProcess().split(",");
										 for (String pers : personapp) {
											if (currentperson.getResume().getName().equals(pers)) {
												ob.setPersonAppState(2);
												 this.getArticleTookService().submit(ob);
												break;
											}else{
												 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
												    ob.setPersonAppState(1);
												    this.getArticleTookService().submit(ob);  
											}
										}
									}else{
										 ob.setShowstate(AppContext.getAccountContext().currentUser().getId()+"：1"); 
										    ob.setPersonAppState(1);
										    this.getArticleTookService().submit(ob);
									}
							}else{
								
							}
						}
						break;
					 }
				 }
			 }
		}
		 DetachedCriteria criteria = this.getPaginationHelper().createQuery(ArticleTook.class);
		 criteria.add(Restrictions.eq("at_examaineState", 1));// 1：未审核
		 criteria.add(Restrictions.eq("showstate",AppContext.getAccountContext().currentUser().getId()+"：1"));//1：显示
		 criteria.add(Restrictions.isNotNull("workFlow"));//审签流程不为空值	
		 criteria.add(Restrictions.eq("personAppState", 1));
		 pagination=this.getPaginationHelper().prepare(pagination);
		 this.setPagination(this.getArticleTookService().listByPage(criteria, pagination));
		 return SUCCESS;
	}
	
	/**
	 * 顺序调整
	 * 
	 */
	public  static List<WorkFlowAndPerson>  sequens(List<WorkFlowAndPerson> ss)
	{
		List<WorkFlowAndPerson>  returnlist=new ArrayList<WorkFlowAndPerson>();
		int slength=ss.size();
		for(int i=0;i<slength;i++)
		{
			  
			for(WorkFlowAndPerson  uo:ss)
			{
				if(uo.getFlowsequen().equals(i+""))
				{
					returnlist.add(uo);
				}
			}
		}
		return returnlist;
		
	}

	@Action("editApplArticleTook")
	public String editApplArticleTook() {
		this.demands=this.getDemandInventoryService().getAllDemandInventory();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		this.tookDepartment=this.getTookDepartmentService().getAllDepartment();
		return SUCCESS;
	}
	
	@Clew(results = { "success" })
	@Action(value = "saveApplArticleTook", results = { @Result(name = "success", type = "redirect", location = "listAppArticleTook") })
	public String saveApplArticleTook() throws SQLException {
		List<WorkFlowAndPerson> wf=this.getWorkFlowAndPersonService().getwWorkFlowAndPersonByArticleTook(articleTook.getWorkFlow(), articleTook);
		List<WorkFlowAndPerson> wfp=sequens(wf);
		for (WorkFlowAndPerson wff : wfp) {
			if("1".equals(wff.getFlowstate())){
				String[] personss=wff.getNodePersonId().split(",");
				for (String persons : personss) {
//					System.out.println(persons);
					if((AppContext.getAccountContext().currentUser().getId()+"").equals(persons)){
						FlowVerifyDetail detail=new FlowVerifyDetail();
						if (upload!=null) {
							this.flowVerifyDetail.setAttach(FileUtil.getInstance().createAttach(upload, uploadFileName));
							detail.setAttach(flowVerifyDetail.getAttach());
						}
						detail.setWorkFlowAndPerson(wff);
						detail.setExecutor(AppContext.getAccountContext().currentUser());
						detail.setResult(flowVerifyDetail.getResult());
						detail.setOpinion(flowVerifyDetail.getOpinion());
						this.saveEimFlowVerifyDetail(detail, articleTook);
						if("5".equals(wff.getType())){
							if (wff.getPersonNum()==1) {
								if (flowVerifyDetail.getResult()==4) {
									wff.setFlowstate("2");
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setAt_examaineState(4);
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
								
									this.setPrompt("提交成功");
									return SUCCESS;
								}else if(flowVerifyDetail.getResult()==1) {
									wff.setFlowstate("2");
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setAt_examaineState(2);
									articleTook.setShowstate("2");
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
									
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}else{
								if (flowVerifyDetail.getResult()==4) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum()-1);
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setAt_examaineState(4);
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
									
									this.setPrompt("提交成功");
									return SUCCESS;
								}else if(flowVerifyDetail.getResult()==1) {
									wff.setPersonNum(wff.getPersonNum()-1);
									this.getWorkFlowAndPersonService().submit(wff);
									
									articleTook.setShowstate("2");
									if (articleTook.getPersonAppProcess()==null || "".equals(articleTook.getPersonAppProcess().trim())) {
										articleTook.setPersonAppProcess(AppContext.getAccountContext().currentUser().getResume().getName());
									}else{
										articleTook.setPersonAppProcess(articleTook.getPersonAppProcess()+","+AppContext.getAccountContext().currentUser().getResume().getName());
									}
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
						
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}
						}else{
							if (wff.getPersonNum()==1) {
								if (flowVerifyDetail.getResult()==4) {
									wff.setFlowstate("2");
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setAt_examaineState(4);
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
								
									this.setPrompt("提交成功");
									return SUCCESS;
								}else if(flowVerifyDetail.getResult()==1) {
									wff.setFlowstate("2");
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setShowstate("2");
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
									
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}else{
								if (flowVerifyDetail.getResult()==4) {
									wff.setFlowstate("2");
									wff.setPersonNum(wff.getPersonNum()-1);
									this.getWorkFlowAndPersonService().submit(wff);
									articleTook.setAt_examaineState(4);
									articleTook.setPersonAppProcess("");
									articleTook.setPersonAppState(2);
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
									
									this.setPrompt("提交成功");
									return SUCCESS;
								}else if(flowVerifyDetail.getResult()==1) {
									wff.setPersonNum(wff.getPersonNum()-1);
									this.getWorkFlowAndPersonService().submit(wff);
								
									articleTook.setShowstate("2");
									if (articleTook.getPersonAppProcess()==null || "".equals(articleTook.getPersonAppProcess().trim())) {
										articleTook.setPersonAppProcess(AppContext.getAccountContext().currentUser().getResume().getName());
									}else{
										articleTook.setPersonAppProcess(articleTook.getPersonAppProcess()+","+AppContext.getAccountContext().currentUser().getResume().getName());
									}
									articleTook.setAt_examainPerson(AppContext.getAccountContext().currentUser().getResume().getName());
									this.getArticleTookService().submit(articleTook);
								
									this.setPrompt("提交成功");
									return SUCCESS;
								}
								break;
							}
						}
					}
				}
			}
		}
		return SUCCESS;
	}
	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
	    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
	    Date dat=new Date();
	    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
	    String date=adf.format(dat);
	    if(alist==null)
	    {//初始的时候调用
		    AutoCode  ac=new AutoCode();
		    ac.setCodeType(type);
		    ac.setCodeValue(type+date+"0000001");
		    ac.setAddValue("0000001");
		    this.getAutoCodeService().save(ac);	
		    return type+date+"0000001";
	    }else{
	    	String vac=alist.getAddValue();
	    	AutoCode alistA=this.getAutoCodeService().getTaskRemind(type);
	    	alistA.setCodeValue(type+date+new AutoCodeAction().addCodes(vac));
	    	alistA.setAddValue(new AutoCodeAction().addCodes(vac));
		    this.getAutoCodeService().submit(alistA);	
		    return type+date+new AutoCodeAction().addCodes(vac);
	    }
	}
	
	public void getConn() throws SQLException{
		ShareEim se=this.getShareEimService().getCode("se01");
		if (se!=null) {
			ClassName=se.getClassName();
			userName=se.getUserName();
			userPass=se.getUserPass();
			address=se.getIp();
			baseName=se.getBaseName();
			url1="jdbc:mysql://"+address+":3306/"+baseName;
		}
		try {
			Class.forName(ClassName);
			con = DriverManager.getConnection(url1, userName, userPass);
			stmt = this.con.createStatement();
			stmt1 = this.con.createStatement();
			stmt2 = this.con.createStatement();
			stmt3 = this.con.createStatement();
			stmt4 = this.con.createStatement();
			stmt5 = this.con.createStatement();
			stmt6 = this.con.createStatement();
			stmt7 = this.con.createStatement();
			stmt8 = this.con.createStatement();
			stmt9 = this.con.createStatement();
			stmt10 = this.con.createStatement();
			stmt11 = this.con.createStatement();
			stmt12 = this.con.createStatement();
			stmt13 = this.con.createStatement();
			stmt14 = this.con.createStatement();
			stmt15 = this.con.createStatement();
			stmt16= this.con.createStatement();
			stmt17 = this.con.createStatement();
			stmt18= this.con.createStatement();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/*
	 * 得到当前时间
	 */
	public String GetTime(){
		String dateString="123";
		try{
		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
		Date currentTime_1=new Date();
		dateString=formatter.format(currentTime_1);
		}catch(Exception e){
		}
		return dateString;
	} 
	
	public void saveEimArtileTook(ArticleTook articleTook) throws SQLException{
		this.getConn();
		//根据物品单号查询物品ID
		rs=stmt.executeQuery("select * from app_articleTook where at_billNumber='"+articleTook.getAt_billNumber()+"'");
		if(!rs.next()){
			rs1=stmt1.executeQuery("select * from app_demandInventory where di_articleName='"+articleTook.getDemands().getDi_articleName()+"'");
			while(rs1.next()){
				String ids=rs1.getString("id");
				rs2=stmt2.executeQuery("select * from flow_flow where name='"+articleTook.getWorkFlow().getName()+"'");
				while(rs2.next()){
					String ids1=rs2.getString("id");
					rs5=stmt6.executeQuery("select * from hr_person where loginCode='"+articleTook.getRecorder().getLoginCode()+"'");
					while(rs5.next()){
						String ids2=rs5.getString("id");
						String sql="insert into app_articleTook(at_billNumber,tookNum,person_id,demands_id,at_applyDate,at_examaineState,appName,type,workFlow_id,recorder_id)" +
								"value('"+articleTook.getAt_billNumber()+"','"+articleTook.getTookNum()+"','"+ids2+"','"+ids+"','"+this.GetTime()+"','"+articleTook.getAt_examaineState()+"','"+articleTook.getAppName()+"','"+articleTook.getType()+"','"+ids1+"','"+ids2+"')";
//						System.out.println(sql);
						stmt3.executeUpdate(sql);
					}
					rs5.close();
				}	
				rs2.close();
			}
			rs1.close();
			rs.close();
			con.close();
		}
			
	}
	public void saveEimFlowAndPerson(WorkFlowAndPerson workFlowAndPerson) throws SQLException{
		this.getConn();
		rs3=stmt4.executeQuery("select * from flow_flow where name='"+workFlowAndPerson.getWorkFlow().getName()+"'");
		if (rs3.next()) {
			String ids=rs3.getString("id");
			rs4=stmt4.executeQuery("select * from app_articleTook where at_billNumber='"+workFlowAndPerson.getArticleTook().getAt_billNumber()+"'");
			if(rs4.next()){
				String ids1=rs4.getString("id");
				String sql="insert into flow_workflowandperson(workFlow_id,nodePersonId,flowstate,flowsequen,type,articleTook_id,personNum)" +
						"values('"+ids+"','"+this.getPerId(workFlowAndPerson)+"','"+workFlowAndPerson.getFlowstate()+"','"+workFlowAndPerson.getFlowsequen()+"','"+workFlowAndPerson.getType()+"','"+ids1+"','"+workFlowAndPerson.getPersonNum()+"')";
				stmt5.executeUpdate(sql);
			}
			rs4.close();
			rs3.close();
			con.close();
		}		
	}
	
	public String getPerId(WorkFlowAndPerson workFlowAndPerson) throws SQLException{
		String perId="";
		Collection<Person> acceptPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(workFlowAndPerson.getNodePersonId().split(",")));
		Iterator<Person> iter = acceptPersons.iterator();
		while(iter.hasNext()) {
			Person person = iter.next();
//			System.out.println(person.getId());
			rs7=stmt7.executeQuery("select * from hr_person where loginCode='"+person.getLoginCode()+"'");
			if(rs7.next()){
				String ids=rs7.getString("id");
				if ("".equals(perId)) {
					perId=ids;
				}else{
					perId=perId+","+ids;
				}
			}
			rs7.close();
		}
		return perId;
	}
	
	
	
	public void saveEimFlowVerifyDetail(FlowVerifyDetail detail,ArticleTook articleTook) throws SQLException{
		this.getConn();
		rs8=stmt8.executeQuery("select * from app_articleTook where at_billNumber='"+articleTook.getAt_billNumber()+"'");
		if (rs8.next()) {
			String ids2=rs8.getString("id");
			rs9=stmt9.executeQuery("select * from flow_workflowandperson where flowsequen='"+detail.getWorkFlowAndPerson().getFlowsequen()+"' and articleTook_id='"+ids2+"'");
			while (rs9.next()) {
				String ids=rs9.getString("id");
				rs10=stmt10.executeQuery("select * from hr_person where loginCode='"+AppContext.getAccountContext().currentUser().getLoginCode()+"'");
				while(rs10.next()){
					String ids1=rs10.getString("id");
					String sql="insert into flow_verifydetail(opinion,result,executor_id,workFlowAndPerson_id,recordTime)" +
						"value('"+detail.getOpinion()+"','"+detail.getResult()+"','"+ids1+"','"+ids+"','"+this.GetTime()+"')";
					stmt11.executeUpdate(sql);
				}
				rs10.close();
			}
			rs9.close();
		}
		rs8.close();
		con.close();
	}
	
	public void saveArticle(ArticleTook articleTook) throws SQLException{
		this.getConn();
		rs12=stmt12.executeQuery("select * from app_articleTook where at_billNumber='"+articleTook.getAt_billNumber()+"'");
		if (rs12.next()) {
			String ids=rs12.getString("id");
			String sql="update app_articleTook set at_examainPerson='"+articleTook.getAt_examainPerson()+"',at_examaineState='"+articleTook.getAt_examaineState()+"',showstate='"+articleTook.getShowstate()+"',personAppProcess='"+articleTook.getPersonAppProcess()+"',personAppState='"+articleTook.getPersonAppState()+"' where id='"+ids+"'";
			stmt13.executeUpdate(sql);
		}
		rs12.close();
		con.close();
	}
	
	public void saveWorkAndPerson(WorkFlowAndPerson wp,ArticleTook articleTook) throws SQLException{
		this.getConn();
		rs14=stmt14.executeQuery("select * from app_articleTook where at_billNumber='"+articleTook.getAt_billNumber()+"'");
		if (rs14.next()) {
			String ids1=rs14.getString("id");
			rs15=stmt15.executeQuery("select * from flow_workflowandperson where flowsequen='"+wp.getFlowsequen()+"' and articleTook_id='"+ids1+"'");
			while (rs15.next()) {
				String ids=rs15.getString("id");
				String sql="update flow_workflowandperson set flowstate='"+wp.getFlowstate()+"', personNum='"+wp.getPersonNum()+"' where id='"+ids+"'";
				stmt16.executeUpdate(sql);
			}
			rs15.close();
		}
		rs14.close();
		con.close();
	}
	public void saveDemand(ArticleTook at,DemandInventory di) throws SQLException{
		this.getConn();
		rs17=stmt17.executeQuery("select * from app_demandinventory where di_articleName='"+at.getDemands().getDi_articleName()+"'");
		if (!rs17.next()) {
			String ids=rs17.getString("id");
			String sql="update app_demandinventory set di_articleNumber='"+di.getDi_articleNumber()+"', di_money='"+di.getDi_money()+"' where id='"+ids+"'";
			stmt18.executeUpdate(sql);
		}
		rs17.close();
		con.close();
	}
	
	@Action(value = "viewAppTook", results = { @Result(name = "error", type = "chain", location = "listArticleTook") })
	public String viewAppTook(){
		if (ids!=null) {
			this.articleTook=this.getArticleTookService().get(ids[0]);
			this.workFlowAndPersons=this.getWorkFlowAndPersonService().getwWorkFlowAndPersonByArticleTook(articleTook.getWorkFlow(), articleTook);
			this.flowVerifyDetails=this.getFlowVerifyDetailService().getAllFlowVerifyDetails();
//			for (WorkFlowAndPerson wfa : workFlowAndPersons) {
//				this.flowVerifyDetails=this.getFlowVerifyDetailService().getFlowVerifyDetails(wfa);
//			}
			return SUCCESS;
		}else{
			this.addActionError("请选择数据！");
			return ERROR;
		}
	}
}
